/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.dromara.hmily.tac.datasource;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.apache.commons.lang3.StringUtils;

/**
 * The type Hmily statement.
 *
 * @param <T> the type parameter
 * @author xiaoyu
 */
public class HmilyStatement<T extends Statement> extends AbstractHmilyStatement<T> {
    
    /**
     * Instantiates a new Hmily statement.
     *
     * @param connectionWrapper the connection wrapper
     * @param targetStatement   the target statement
     * @param targetSQL         the target sql
     */
    public HmilyStatement(final AbstractHmilyConnection connectionWrapper, final T targetStatement, final String targetSQL) {
        super(connectionWrapper, targetStatement, targetSQL);
    }
    
    /**
     * Instantiates a new Hmily statement.
     *
     * @param connectionWrapper the connection wrapper
     * @param targetStatement   the target statement
     */
    public HmilyStatement(final AbstractHmilyConnection connectionWrapper, final T targetStatement) {
        this(connectionWrapper, targetStatement, null);
    }
    
    @Override
    public HmilyConnection getConnectionWrapper() {
        return (HmilyConnection) super.getConnectionWrapper();
    }
    
    @Override
    public ResultSet executeQuery(final String sql) throws SQLException {
        setTargetSQL(sql);
        return getTargetStatement().executeQuery(sql);
    }
    
    @Override
    public int executeUpdate(final String sql) throws SQLException {
        setTargetSQL(sql);
        return getTargetStatement().executeUpdate(sql);
    }
    
    @Override
    public int executeUpdate(final String sql, final int autoGeneratedKeys) throws SQLException {
        setTargetSQL(sql);
        return getTargetStatement().executeUpdate(sql, autoGeneratedKeys);
    }
    
    @Override
    public int executeUpdate(final String sql, final int[] columnIndexes) throws SQLException {
        setTargetSQL(sql);
        return getTargetStatement().executeUpdate(sql, columnIndexes);
    }
    
    @Override
    public int executeUpdate(final String sql, final String[] columnNames) throws SQLException {
        setTargetSQL(sql);
        return getTargetStatement().executeUpdate(sql, columnNames);
    }
    
    @Override
    public boolean execute(final String sql) throws SQLException {
        setTargetSQL(sql);
        return getTargetStatement().execute(sql);
    }
    
    @Override
    public boolean execute(final String sql, final int autoGeneratedKeys) throws SQLException {
        setTargetSQL(sql);
        return getTargetStatement().execute(sql, autoGeneratedKeys);
    }
    
    @Override
    public boolean execute(final String sql, final int[] columnIndexes) throws SQLException {
        setTargetSQL(sql);
        return getTargetStatement().execute(sql, columnIndexes);
    }
    
    @Override
    public boolean execute(final String sql, final String[] columnNames) throws SQLException {
        setTargetSQL(sql);
        return getTargetStatement().execute(sql, columnNames);
    }
    
    @Override
    public void addBatch(final String sql) throws SQLException {
        String targetSQL = getTargetSQL();
        if (StringUtils.isNotBlank(targetSQL)) {
            targetSQL += "; " + sql;
            setTargetSQL(targetSQL);
        } else {
            setTargetSQL(sql);
        }
        getTargetStatement().addBatch(sql);
    }
    
    @Override
    public int[] executeBatch() throws SQLException {
        return getTargetStatement().executeBatch();
    }
}
